﻿String.prototype.TakeFirst = function TakeFirst(min, max, appendix)
{
	var sb = new StringBuilder(),
		sbWord = new StringBuilder();
	for (var i = 0; i <= this.length; ++i)
	{
		if (i == this.length || Char.IsWhiteSpace(this.charAt(i)))
		{
			if (sbWord.get_Length() > 0)
			{
				if (sb.get_Length() + sbWord.get_Length() <= max)
				{
					if (sb.get_Length() > 0)
						sb.Append(" ");
					sb.Append(sbWord.toString());
					sbWord.Clear();
				}
				else
				{
					if (sb.get_Length() < min)
					{
						if (sb.get_Length() > 0)
							sb.Append(" ");
						sb.Append(sbWord.toString().substr(0, max - sb.get_Length()));
					}
					sb.Append(appendix);
					break;
				}
			}
		}
		else
		{
			sbWord.Append(this.charAt(i));
		}
	}
	return sb.toString();
}
